package com.wyd.System.Controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyd.System.Bean.Users;
import com.wyd.System.Service.UsersService;
import com.wyd.handler.BusinessException;
import com.wyd.response.Result;
import com.wyd.response.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 文永达
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/System/users")
//@CrossOrigin
@Api(value = "用户管理")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @GetMapping("/findUsers")
    @ApiOperation(value = "查询所有的用户信息",notes = "")
    public Result findUsers(){
        List<Users> list = usersService.list();
        return Result.ok().data("users",list);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个用户",notes = "通过ID查询信息")
    public Result getUserById(@PathVariable("id") Long id){
        Users users=usersService.getById(id);
        if(users!=null){
            return Result.ok().data("users",users);
        }else {
            throw new BusinessException(ResultCode.USER_NOT_FOUND_EXCEPTION.getCode(),ResultCode.USER_NOT_FOUND_EXCEPTION.getMessage());
        }
    }

    @GetMapping("/findUserList")
    public Result findUserList(@RequestParam(required = true,defaultValue = "1")Integer current,
                               @RequestParam(required = true,defaultValue = "6")Integer size){
        Page<Users> page = new Page<>(current,size);
        Page<Users> userPage=usersService.page(page);
        long total=userPage.getTotal();
        List<Users> records = userPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }
}

